package DoubleList;

import java.util.LinkedList;
import java.util.Objects;

public class List {
    private Node first;
    private Node last;
    private int size;

    //链表的长度
    public String size(){
        return "链表的长度为：" + size;
    }

    //添加元素
    public String add(Object obj){
        Node node = new Node(null, obj, null);
        if (first == null){
            first =node;
            last = node;
            size ++;
            return "成功添加元素：" + obj;
        }else {
            last.next = node;
            node.prev = last;
            last = node;
            size++;
            return "成功添加元素：" + obj;
        }
    }

    //删除元素
    public String Remove(Object o){
        if (first == null || last ==null){
            return "链表为空！";
        }
        if (Objects.equals(first.value,o)){
            first.next.prev = null;
            first = first.next;
            size -- ;
            return "成功删除元素：" + o;
        }
        if (Objects.equals(last.value,o)){
            last.prev.next = null;
            last = last.prev;
            size -- ;
            return "成功删除元素：" + o;
        }
        Node t = first.next;
        while (!Objects.equals(t.value,o)){
            t =t.next;
            if (t == null){
                return "未找到元素：" + o;
            }
        }
        t.prev.next = t.next;
        t.next.prev = t.prev;
        size -- ;
        return "成功删除元素：" + o;
    }

    //链表为空
    public String isEmpty() {
        if (size <= 0){
           return "链表为空！";
        }else {
            return "链表不为空！";
        }
    }

    //链表是否有该元素
    public String contains(Object o) {
        Node t = first;
        while (! Objects.equals(t.value,o)){
            t = t.next;
            if (t == null){
                return "链表中没有该元素!" + o;
            }
        }
        return "成功找到元素：" + o;
    }

    //指定下标的元素
    public Object get(int index) {
        if (index < 0 || size < 0){
            System.out.println("链表中没有元素！");
        }
        Node t = first;
        for (int i = 0; i <index ; i++) {
            t = t.next;
        }
        return "指定位置的元素为：" + t.value;
    }

    //获得元素的下标
    public String indexOf(Object o) {
        if (size <= 0){
            System.out.println("链表中没有元素！");
        }
        int count = 0;
        Node t = first;
        while (! Objects.equals(t.value,o)){
            t =t.next;
            if (t == null){
                System.out.println("链表中没有该元素！");
            }
            count ++;
        }
        return "该元素的下标为：" + count;
    }

    //字符串输出
    public String toString(){
        StringBuilder sb = new StringBuilder("Node[");
        Node t = first;
        while (t.next != null){
            sb.append(t.value + ",");
            t = t.next;
        }
        sb.append(last.value + "]");
        return sb.toString();
    }
    class Node{
        private Object value;
        private Node prev;
        private Node next;

        public Node(Node prev,Object value,  Node next) {
            this.prev = prev;
            this.value = value;
            this.next = next;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Node node = (Node) o;
            return Objects.equals(value, node.value) && Objects.equals(prev, node.prev) && Objects.equals(next, node.next);
        }

        @Override
        public int hashCode() {
            return Objects.hash(value, prev, next);
        }
    }
}
